module Playlists

  private
  # Generates a WPL playlist, used inside the class.
  # 
  def playlist_wpl(opts={}) #:doc:
    playlist = "";
    xml = Builder::XmlMarkup.new(:target => playlist, :indent => (opts[:indent] || 4));
    xml.instruct!("wpl", :version => "1.0");
    xml.smil do
      xml.head do
        xml.meta(:name => "Generator", :content => @generator);
        xml.author;
        xml.title(opts[:title] || "RubyMusicBank");
      end
      xml.body do
        xml.seq do
          each{|song, mp3| xml.media(:src => get_path(opts) + song);}
        end
      end
    end
    write_file(opts[:save], playlist) if opts[:save];
    playlist;
  end
  
  # Generates a M3U playlist, used inside the class.
  # 
  def playlist_m3u(opts={})  #:doc:
    playlist = "#EXTM3U\n\n";
    each do |song, mp3|
      song_name = (opts[:format]) ? apply_format(song, opts[:format]) : song.sub(@exts, "");
      time_secs = mp3.length.to_i.to_s;
      path = get_path(opts) + song;
      playlist << "#EXTINF:#{time_secs},#{song_name}\n"
      playlist << path + "\n\n";
    end
    write_file(opts[:save], playlist) if opts[:save];
    playlist;
  end
  
  # Generates a PLS playlist, used inside the class.
  # 
  def playlist_pls(opts={})  #:doc:
    playlist = "[playlist]\n";
    playlist << "NumberOfEntries=#{@songs.length.to_s}\n";
    index = 1;
    each do |song, mp3|
      song_name = (opts[:format]) ? apply_format(song, opts[:format]) : song.sub(@exts, "");
      time_secs = mp3.length.to_i.to_s;
      path = get_path(opts) + song;
      playlist << "File#{index.to_s}=#{path}\n";
      playlist << "Title#{index.to_s}=#{song_name}\n";
      playlist << "Length#{index.to_s}=#{time_secs}\n";
      index += 1;
    end
    write_file(opts[:save], playlist) if opts[:save];
    playlist;
  end

end